<?php
if(!defined('ACCESS')) {exit('Access denied.');}
class Customer extends Base {
	// 表名
	private static $table_name = 'customer_info';
	// 查询字段
	private static $columns = '*';

	public static function getTableName(){
		return parent::$table_prefix.self::$table_name;
	}
	
	//列表 
	public static function getAllCustomer() {
		$db=self::__instance();
		$sql="select ".self::$columns."  from ".self::getTableName()." ";
		$list = $db->query($sql)->fetchAll();
		if ($list) {
			return $list;
		}
		return array ();
	}

	
	public static function addCustomer($customer_data) {
		if (! $customer_data || ! is_array ( $customer_data )) {
			return false;
		}

		$db=self::__instance();
		$id = $db->replaceinsert ( self::getTableName(), $customer_data );
		return $id;
	}

	public static function getCustomerById($customer_id) {
		if (! $customer_id || ! is_numeric ( $customer_id )) {
			return false;
		}
		$db=self::__instance();
		$condition['customer_id'] = $customer_id;
		$list = $db->select ( self::getTableName(), self::$columns, $condition );
		if ($list) {
			return $list [0];
		}
		return array ();
	}
	
	
	public static function count($search_val) {
		$db=self::__instance();
		
		
		if($search_val != ''){
			$where .=  " where customer_name like '%$search_val%' ";
		}
		$sql = " SELECT count(*) as count_num from `osa_customer_info` ".$where;
		$list = $db->query ($sql)->fetchAll();
		return $list["0"]["count_num"];
	}
	
	public static function getCusListBySearch($search_val) {
		$db=self::__instance();
		
		if($search_val != ''){
			$where .=  " where customer_name like '%$search_val%' ";
		}
		$sql = " SELECT * from `osa_customer_info` ".$where;

		$list = $db->query ($sql)->fetchAll();
		return $list;
	}
	
	public static function getCustomers($search_val,$start,$page_size) {
		$db=self::__instance();

		if($search_val != ''){
			$where .=  " where customer_name like '%$search_val%' ";
		}

		$where .= " ORDER BY customer_id asc ";
		$where .= " LIMIT $start , $page_size ";
		$sql = " SELECT * from `osa_customer_info` ".$where;
		$list = $db->query ($sql)->fetchAll();
		if ($list) {
			return $list;
		}
		return array ();
	}
	
	public static function getCustomersVisitList($start,$page_size) {
		$db=self::__instance();
	
		$where .= " LIMIT $start , $page_size ";
		$sql = " SELECT a.machine_id ,a.serial_number ,a.host_number,a.engineer,a.last_vistor_time FROM  osa_customer_info  a ".$where;
		
		$list = $db->query ($sql)->fetchAll();
		if ($list) {
			return $list;
		}
		return array ();
	}
	
	public static function getCustomersVisitListCount() {
		$db=self::__instance();
	
		$sql = " SELECT count(*) as count_num FROM  osa_customer_info  a ";
		
		$list = $db->query ($sql)->fetchAll();
		return $list["0"]["count_num"];

	}
	
	public static function visitListcount() {
		$db=self::__instance();
	
		$sql = "SELECT count(a.machine_id) as num FROM  osa_customer_info a ,osa_visitor  b
WHERE  a.machine_id = b.id";
	
		$num = $db->query($sql)->fetchAll();
		return $num['0']['num'];
	}
	
   	/** 
	 * 创建(导出)Excel数据表格 
	 * @param  array   $list 要导出的数组格式的数据 
	 * @param  string  $filename 导出的Excel表格数据表的文件名 
	 * @param  array   $header Excel表格的表头 
	 * @param  array   $index $list数组中与Excel表格表头$header中每个项目对应的字段的名字(key值) 
	 * 比如: $header = array('编号','姓名','性别','年龄'); 
	 *       $index = array('id','username','sex','age'); 
	 *       $list = array(array('id'=>1,'username'=>'YQJ','sex'=>'男','age'=>24)); 
	 * @return [array] [数组] 
	 */  
	public static function createtable($list,$filename,$header=array(),$index = array()){
		header("Content-type:application/vnd.ms-excel");
		header("Content-Disposition:filename=".$filename.".xls");
		$teble_header = implode("\t",$header);
		$strexport = $teble_header."\r";
		foreach ($list as $row){
			foreach($index as $val){
				$strexport.=$row[$val]."\t";
			}
			$strexport.="\r";
		
		}
		$strexport=iconv('UTF-8',"GB2312//IGNORE",$strexport);
		exit($strexport);
	}
}
